<?php

namespace services\common;

use Yii;
use common\components\Service;
use common\enums\HardwareNameEnum;
use common\enums\HardwareTypeEnum;
use common\enums\StatusEnum;
use common\queues\ReceiptPrinterJob;
use common\models\common\HardwareConfig;
use common\models\hardware\FeiE;
use common\models\hardware\YiLianYun;

/**
 * Class HardwareService
 * @package services\common
 * @author YiiFrame <21931118@qq.com>
 */
class HardwareService extends Service
{
    /**
     * @var bool
     */
    public $queueSwitch = false;

    /**
     * 打印单个
     *
     * @param $id
     */
    public function receiptPrinterById($id, $data)
    {
        // 是否进入队列
        if ($this->queueSwitch == true) {
            $messageId = Yii::$app->queue->push(new ReceiptPrinterJob([
                'config' => $this->findById($id),
                'data' => $data,
            ]));

            return $messageId;
        } else {
            return $this->receiptPrinter($this->findById($id), $data);
        }
    }

    /**
     * 执行打印
     *
     * @param $model
     * @param $data
     * @return bool
     */
    public function receiptPrinter(HardwareConfig $config, $data)
    {
        if (empty($config) || empty($data)) {
            return false;
        }

        $model = $this->getModel($config->name, $config->data);
        switch ($config->name) {
            case HardwareNameEnum::YI_LIAN_YUN :
                Yii::$app->services->printerYiLianYun->text($data, '', $model);
                break;
            case HardwareNameEnum::FEI_E :
                Yii::$app->services->printerFeiEYun->print($data, $model);
                break;
        }

        return true;
    }

    /**
     * 获取对应的配置模型
     *
     * @param $name
     * @param $data
     * @return string|\yii\base\Model|FeiE|YiLianYun
     */
    protected function getModel($name, $data)
    {
        $model = HardwareNameEnum::getModelValue($name);
        $model->attributes = $data;

        return $model;
    }

    /**
     * @param $type
     * @return array|\yii\db\ActiveRecord[]
     */
    public function findByType($type, $merchant_id)
    {
        return HardwareConfig::find()
            ->where(['type' => $type])
            ->andWhere(['status' => StatusEnum::ENABLED])
            ->andFilterWhere(['merchant_id' => $merchant_id])
            ->all();
    }

    /**
     * @param $id
     * @return array|\yii\db\ActiveRecord|null|HardwareConfig
     */
    public function findById($id)
    {
        return HardwareConfig::find()
            ->where(['id' => $id])
            ->andWhere(['status' => StatusEnum::ENABLED])
            ->one();
    }

    /**
     * @param array $name
     * @param $merchant_id
     * @return array|\yii\db\ActiveRecord[]
     */
    public function findAutoByType($type, $merchant_id)
    {
        return HardwareConfig::find()
            ->where([
                'type' => $type,
                'merchant_id' => $merchant_id,
                'status' => StatusEnum::ENABLED,
                'is_auto' => StatusEnum::ENABLED,
            ])
            ->all();
    }

    /**
     * @param $id
     * @return FeiE|YiLianYun|string|\yii\base\Model
     */
    public function getDataById($id)
    {
        $config = $this->findById($id);

        return $this->getModel($config->name, $config->data);
    }
}